package com.mailsystem.www.dao;

import java.io.Serializable;
import java.lang.reflect.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BaseDaoImpl<T> implements BaseDao<T> {
    private Class<T> gclass;

    public BaseDaoImpl() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] types = pt.getActualTypeArguments();
        gclass = (Class<T>) types[0];
    }

    @Override
    public void save(T entity, Connection conn) {
        Class<?> class1 = entity.getClass();
        String tableName = class1.getSimpleName();
        String getName = null;
        Field[] df = class1.getDeclaredFields();
        String sql="insert into"+tableName+"(";
    }

    @Override
    public void delete(T entity, Connection conn) {

    }

    @Override
    public void update(T entity, Connection conn) throws SQLException {
        Class<?> class1 = entity.getClass();
        String tableName = class1.getSimpleName();
        String getName = null;
        // 获得成员属性
        Field[] df = class1.getDeclaredFields();
        // 拼接sql语句
        // update User set id=?,uname=?,upwd=?,money=? where id=?
        String sql = "update " + tableName + " set ";
        for (int i = 0; i < df.length; i++) {
            Field field = df[i];
            getName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);

            sql += field.getName() + "=?";
            if (i < df.length - 1) {
                sql += ",";
            } else {
                sql += " where id=?";
            }
        }
        // update User set id=?,uname=?,upwd=?,money=? where id=?
        // 填充占位符
        PreparedStatement ps = conn.prepareStatement(sql);
        // 存放条件表达式 即id,数组下标第一
        String where;
        for (int i = 0; i < df.length; i++) {
            Field field = df[i];
            getName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            // 存放条件表达式 即id,数组下标第0位,填充第length+1位占位符
            try {
                if (i == 0) {
                    where = getName;
                    // 反射调用getId方法并把值拼接到最后一位

                    ps.setObject(df.length + 1, class1.getDeclaredMethod(where).invoke(entity));

                }
                Method method = class1.getDeclaredMethod(getName);

                ps.setObject(i + 1, method.invoke(entity));
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        int executeUpdate = ps.executeUpdate();
        System.out.println(sql);

    }

    @Override
    public T findbyid(Serializable id, Connection conn) {
        String sql = "select *from " + gclass.getSimpleName() + " where id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, id);
            ResultSet rs = ps.executeQuery();
            T t;
            t = (T) gclass.newInstance();
            if (rs.next()) {
                Field[] df = gclass.getDeclaredFields();
                for (int i = 0; i < df.length; i++) {
                    Field f = df[i];
                    // 拿到属性的值
                    Object value = rs.getObject(f.getName());
                    // 拿到setter方法
                    String setter = "set" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);
                    // 获得对象的属性
                    Method method = gclass.getMethod(setter, value.getClass());
                    method.invoke(t, value);
                }
                return t;
            }
        } catch (SQLException | NoSuchMethodException | IllegalAccessException | InstantiationException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<T> findAll(Connection conn) {
        return null;
    }
}
